﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Lab1_Mochulskiy_.Attributes;

namespace Lab1_Mochulskiy_.Occurances
{
    public abstract class OccurancePatternBase : ICriteriaOccurancePattern
    {
        
        public abstract int MinWeight { get ; }
        public abstract int MaxWeight { get; }
        public abstract int MaxSpeed { get; }
        public abstract int WheelsCount { get; }
        public abstract string Name { get; }


        [OccuranceCondition]
        protected virtual bool IsWeightMatch(Criteria criteria)
        {
            if (criteria.Weight >= MinWeight && criteria.Weight <= MaxWeight)
                return true;

            return false;
        }

        [OccuranceCondition]
        protected virtual bool IsMaxSpeedMatch(Criteria criteria)
        {
            if (criteria.MaxSpeed >= MaxSpeed - 10 && criteria.MaxSpeed <= MaxSpeed + 10)
                return true;

            return false;
        }

        [OccuranceCondition]
        protected virtual bool IsWheelsCountMatch(Criteria criteria)
        {
            if (criteria.WheelCount == WheelsCount)
                return true;

            return false;
        }

        public int GetOccuranceCount(Criteria criteria)
        {
            int counter = 0;

            var mi = this.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
            foreach (var method in mi)
            {
                if (Attribute.IsDefined(method, typeof (OccuranceConditionAttribute)))
                {
                    if ((bool)method.Invoke(this, new object[] {criteria}))
                    {
                        counter++;
                    }
                }
            }

            return counter;
        }
    }
}
